package com.itheima.edu.info.mannager.dao;

import com.itheima.edu.info.mannager.domain.Teacher;

import java.util.Scanner;

public class TeacherDao {
    //创建一个长度为五的数组容器
    private static Teacher[] teachers = new Teacher[5];

    //1.添加学生功能
    public boolean addTeacher(Teacher t) {
        int index = -1;
        for (int i = 0; i < teachers.length; i++) {
            Teacher teacher = teachers[i];
            if (teacher == null) {
                index = i;
                break;
            }
        }

        if (index == -1) {
            return false;//表示没有空间,满了
        } else {
            teachers[index] = t;
            return true;
        }

    }

    public Teacher[] findAllTeacher() {
        return teachers;
    }

    public static void deleteTeacherById(String id) {
       //1.查询id在数组中的索引值位置
       int index = getIndex(id);
       //2.将索引值位置的元素,使用null替换
        teachers[index] = null;
    }

    public static  int getIndex(String id) {
    int index = -1 ;
        for (int i = 0; i < teachers.length; i++) {
            Teacher t = teachers[i];
            if (t != null &&t.getSid().equals(id)){
                index = i ;
                break ;
            }
        }
        return index ;
    }

    public void updateTeacher(String id, Teacher newTeacher) {
        int index = getIndex(id);
        teachers[index] = newTeacher;
    }
}


        

